﻿namespace com.oakwoodsys.Extensions.ObjectExtensions
{
    //
    // Copyright © Sidney Mark Croy 2008-2011 GNU Lesser General Public License
    //
    // This file is part of QSystem.
    //
    // QSystem is free software; you can redistribute it and/or modify
    // it under the terms of the GNU Lesser General Public License as published by
    // the Free Software Foundation; either version 3 of the License, or
    // (at your option) any later version.
    //
    // QSystem is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    // GNU Lesser General Public License for more details.
    //
    // You should have received a copy of the GNU Lesser General Public License
    // along with this program. If not, see <http://www.gnu.org/licenses/>.
    //
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Security.Cryptography;
    using System.Text;
    using System.Xml.Serialization;

    using com.oakwoodsys.Extensions.ByteExtensions;
    using com.oakwoodsys.Extensions.StreamExtensions;

    [CLSCompliant( true )]
    public static class ObjectExtensions
    {
        #region Methods

        public static Int32 ComputeHashCodeForSerializableObject( this object o )
        {
            try
            {
                using ( Stream stream = o.Serialize( ) )
                {
                    byte[ ] b = MD5.Create( ).ComputeHash( stream );
                    StringBuilder sb = new StringBuilder( );
                    for ( int i = 0; i < b.Length; i++ )
                        sb.Append( b[ i ].ToString( "X2" ) );
                    string s = sb.ToString( );
                    sb = null;
                    return s.GetHashCode( );
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Determines whether the current instance implements the specified interface or class. Returns true if the instance implements the specified interface or class, otherwise false.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="type">The System.Type object for a type.</param>
        /// <returns>true if the instance implements the specified interface or class, otherwise false.</returns>
        //public static bool Implements( this object o, Type type )
        //{
        //    if ( o == null || type == null )
        //        return false;
        //    return type.IsAssignableFrom( o.GetType( ) );
        //}
        public static Stream Serialize( this object o )
        {
            Stream s = null;
            using ( Stream stream = new MemoryStream( ) )
            {
                BinaryFormatter f = new BinaryFormatter( );
                f.Serialize( stream, o );
                byte[ ] b = stream.ToArray( );
                s = new MemoryStream( b );
                return s;
            }
        }

        /// <summary>
        /// Serializes the specified System.Object based upon the type specified with an empty namespace. Returns the serialized object as an XML formateed string.
        /// </summary>
        /// <typeparam name="T">Specifies the type of object to serialize.</typeparam>
        /// <param name="o"></param>
        /// <returns>Serializes object as an XML string.</returns>
        public static string SerializeToXml<T>( this object o )
        {
            return o.SerializeToXml( typeof( T ) );
        }

        /// <summary>
        /// Serializes the specified System.Object based upon the specified type with an empty namespace. Returns the serialized object as an XML formatted string.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="type">Specifies the type of object to serialize.</param>
        /// <returns>Serializes object as an XML string.</returns>
        public static string SerializeToXml( this object o, Type type )
        {
            using ( MemoryStream stream = new MemoryStream( ) )
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces( );
                ns.Add( string.Empty, string.Empty );
                XmlSerializer s = new XmlSerializer( type );
                s.Serialize( stream, o, ns );
                byte[ ] b = stream.ToArray( );
                return b.GetString( );
            }
        }

        #endregion Methods
    }
}